/*
Source : https://leetcode.com/problems/happy-number/
Author : nflush@outlook.com
Date   : 2016-07-15
*/

/*
202. Happy Number
 ?  

Question Editorial Solution  
 My Submissions 




?Total Accepted: 78317
?Total Submissions: 208965
?Difficulty: Easy



Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.



Subscribe to see which companies asked this question
*/
class Solution {
private:
    int calc(int n)
    {
        int result = 0;
        int sq = 0;
        for (; n>0; n/=10) {
            sq = n%10;
            result += (sq * sq);
        }
        return result;
    }
public:
    bool isHappy(int n) {
        #define T true
        #define F false
        const static bool a[1000] = {
                F,T,F,F,F,F,F,T,F,F,
                T,F,F,T,F,F,F,F,F,T,
                F,F,F,T,F,F,F,F,T,F,
                F,T,T,F,F,F,F,F,F,F,
                F,F,F,F,T,F,F,F,F,T,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,T,F,
                T,F,F,F,F,F,F,F,F,T,
                F,F,T,F,F,F,T,F,F,F,
                F,T,F,F,T,F,F,T,F,F,
                T,F,F,T,F,F,F,F,F,T,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,T,
                T,F,F,T,F,F,F,F,F,T,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,T,F,F,
                F,F,F,F,F,F,T,F,F,F,
                F,F,F,F,F,F,F,F,T,F,
                T,F,T,T,F,F,F,F,F,F,
                F,F,F,T,F,F,F,F,T,F,
                F,F,F,F,F,F,F,F,F,T,
                F,F,F,F,F,F,T,F,F,F,
                T,F,F,F,F,F,T,F,F,T,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,T,T,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                T,F,F,F,F,F,F,F,F,F,
                F,T,F,T,F,F,F,F,F,F,
                F,T,T,F,F,F,F,F,F,F,
                T,F,F,T,F,F,F,F,F,T,
                T,F,F,F,F,F,T,F,F,T,
                F,T,F,F,F,F,F,F,T,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,T,F,F,F,
                F,F,T,F,F,T,F,T,T,F,
                F,F,F,F,F,F,T,F,F,T,
                F,F,F,T,F,F,T,F,F,F,
                F,T,T,F,F,F,F,T,F,F,
                F,F,F,F,T,F,F,F,F,T,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                T,F,F,F,F,F,T,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,T,F,F,F,F,T,
                F,F,F,F,F,F,F,F,T,F,
                F,F,F,F,F,F,F,T,F,F,
                T,F,F,F,F,F,T,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,T,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,T,F,F,F,
                F,F,F,T,F,T,T,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,T,F,
                F,F,F,F,F,F,F,T,F,F,
                F,F,T,T,F,F,F,F,F,F,
                F,F,T,F,F,T,F,T,T,F,
                F,F,F,F,T,F,F,F,F,T,
                F,F,F,T,F,T,T,F,F,F,
                F,F,F,F,F,T,F,F,F,F,
                F,T,F,T,F,F,F,F,F,F,
                T,F,F,T,F,F,F,F,F,F,
                F,F,F,F,T,F,F,F,F,F,
                T,F,F,F,F,F,F,F,F,T,
                F,F,F,F,F,F,T,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,F,F,T,F,F,T,
                F,F,F,F,F,F,F,F,T,F,
                F,F,F,F,F,F,F,F,F,F,
                F,T,F,T,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,T,F,F,F,F,F,
                T,F,F,T,F,F,F,F,F,F,
                F,F,T,F,F,F,T,F,F,F,
                F,F,F,F,F,F,F,F,T,F,
                T,F,F,F,F,F,F,F,F,F,
                F,F,F,T,F,F,T,F,F,F,
                F,F,F,F,F,F,F,T,F,F,
                F,F,F,F,F,F,F,F,F,F,
                T,F,F,T,F,F,F,F,F,F,
                F,F,F,F,T,F,F,F,F,F,
                F,T,F,F,F,F,F,F,T,F,
                F,F,F,F,F,F,F,F,F,T,
                F,T,F,F,T,F,F,T,F,F,
                T,F,T,T,F,F,F,F,F,F,
                F,T,F,T,F,F,F,F,F,F,
                F,T,T,F,F,F,F,T,F,F,
                T,F,F,F,F,F,T,F,F,F,
                F,F,F,F,F,F,F,F,F,F,
                F,F,F,F,T,F,F,F,F,F,
                T,F,F,T,F,F,F,F,F,F,
                F,F,F,F,F,F,F,F,F,T,
                F,F,F,F,F,F,F,F,T,F};
        while(n > 1000){
            n = calc(n);
        }
        return a[n];
    }
};

int calc(int x)
{
    int ret = 0;
    while (x > 0){
        ret += (x%10) * (x%10);
        x /=10;
    }
    return ret;
}

int main(int argc, char *argv[])
{
    for (int i= 1; i < 1000; i++){
        printf("%d:%d\n", i, calc(i));
    }
    return 0;
}